﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using chesslibrary.cs;

namespace ChessGui
{
    public delegate void ChessButtonHandler(cell_chess cell);
    public class Fields : IEnumerable
    {
        private ChessButton[,] buttons;

        public event ChessButtonHandler onSelect;
        public event ChessButtonHandler onMove;

        public ChessButton this[cell_chess cell]
        {
            get
            {
                return buttons[cell.Col - 1, cell.Row - 1];
            }
        }

        public IEnumerator GetEnumerator()
        {
            return buttons.GetEnumerator();
        }

        private void RaiseSelectEvent(cell_chess cell)
        {
            if (onSelect != null)
            {
                onSelect(cell);
            }
        }

        private void RaiseMoveEvent(cell_chess cell)
        {
            if (onMove != null)
            {
                onMove(cell);
            }
        }
        private void button_Click(object Sender, System.EventArgs e)
        {
            ChessButton b = (ChessButton)Sender;
            if (b.BackColor == Color.Yellow)
            {
                RaiseMoveEvent(b.Cell);
            }
            else
            {
                RaiseSelectEvent(b.Cell);
            }
        }
        public Fields()
        {
            buttons = new ChessButton[8, 8];
            for (int lRow = 0; lRow < 8; lRow++)
            {
                for (int lCol = 0; lCol < 8; lCol++)
                {
                    buttons[lRow, lCol] = new ChessButton(lCol, lRow);
                    buttons[lRow, lCol].Location = new Point(lRow * 70, (7 - lCol) * 70);
                    buttons[lRow, lCol].Size = new Size(70, 70);
                    buttons[lRow, lCol].Click += new System.EventHandler(this.button_Click);
                    if (buttons[lRow, lCol].Cell.IsDark)
                    {
                        buttons[lRow, lCol].BackColor = Color.Blue;
                    }
                    else
                    {
                        buttons[lRow, lCol].BackColor = Color.DarkGreen;
                    }
                }
            }
        }
    }
}
